//
//  GYSaleDetailBtomView.m
//  GYDemo
//
//  Created by Mrzhang on 2018/4/9.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYSaleDetailBtomView.h"
#import "Head.h"
#import "GYBtnView.h"

@interface GYSaleDetailBtomView ()
{
    BOOL isCollect;
}
@property(nonatomic,strong)UIButton* putBtn;
@property(nonatomic,strong)UIButton* orderBtn;
@end
@implementation GYSaleDetailBtomView

-(UIButton *)putBtn{
    
    if (!_putBtn) {
        _putBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _putBtn.backgroundColor=HEXCOLOR(@"#F5A623");
        [_putBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
        [_putBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _putBtn.titleLabel.font=SYSTEMFONT(15);
        _putBtn.frame=CGRectMake(WFCGFloatX(143), 0, WFCGFloatX(116), 45);
        @WeakSelf;
        
        [_putBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            selfp.btnCallBack(4);
        }];
        
    }
    return _putBtn;
}

-(UIButton *)orderBtn{
    
    if (!_orderBtn) {
        _orderBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _orderBtn.backgroundColor=HEXCOLOR(@"#B81A08");
        [_orderBtn setTitle:@"立即购买" forState:UIControlStateNormal];
        [_orderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _orderBtn.titleLabel.font=SYSTEMFONT(15);
        _orderBtn.userInteractionEnabled=YES;
        _orderBtn.frame=CGRectMake(WFCGFloatX(143)+WFCGFloatX(116), 0, WFCGFloatX(116), 45);
        @WeakSelf;
        [_orderBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            selfp.btnCallBack(5);
        }];
    }
    return _orderBtn;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"judgeProductIsCollected"];
        self.userInteractionEnabled=YES;
        [self addSubview:self.putBtn];
        [self addSubview:self.orderBtn];
        [self initView];
    }
    return self;
}
-(void)initView
{
    float ww=WFCGFloatX(143)/3;
    NSArray* titleArr=@[@"收藏",@"购物车"];
    NSArray* imageArr=@[@"Star2",@"gouwuche"];
    NSInteger cout=[GYCartViewModel shareInterface].datas.count;
    NSString* stt=cout==0?@"":[NSString stringWithFormat:@"%ld",cout];
    for (int i=0; i<2; i++)
    {
        GYBtnView* view=[[GYBtnView alloc] initWithFrame:CGRectMake(ww*i+20, 0, ww, 45) titleStr:titleArr[i] image:imageArr[i] count:i==2?stt:@""];
        view.tag=300+i;
        [self addSubview:view];

        @WeakSelf;
        if (i!=0)
        {
            [view addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                selfp.btnCallBack(i+1);
            }];
        }else
        {
            [view addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                [selfp butnPress];
            }];
        }
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    [self requestCheckCollect];
    
    NSInteger cout=[GYCartViewModel shareInterface].datas.count;
    NSString* stt=cout==0?@"":[NSString stringWithFormat:@"%ld",cout];
    GYBtnView* view=[self viewWithTag:302];
    [view setCountLabHHH:stt];
}

-(void)butnPress
{

    if (![[GYLoginViewModel shareInterface] loginstatus]) {
        [[GCPresenter shareInterface] loadLogin];
        return;
    }

    if (!isCollect)
    {
        GYBtnView* view=[self viewWithTag:300];
        [view setBtnImage:@"Star"];
        isCollect=YES;
        [FTIndicator showSuccessWithMessage:@"收藏成功"];
    }else
    {
        GYBtnView* view=[self viewWithTag:300];
        [view setBtnImage:@"Star2"];
        isCollect=NO;
        [FTIndicator showSuccessWithMessage:@"取消收藏"];
    }
}

-(void)requestCheckCollect
{
    NSMutableDictionary* para=[[NSMutableDictionary alloc] init];
    [para setObject:@"1" forKey:@"type"];
    [para setObject:_passID forKey:@"productid"];
    @WeakSelf;
    NSString *interface = @"judgeProductIsCollected";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        if ([info isKindOfClass:[NSDictionary class]]) {
            if ([[info objectForKey:@"status"] integerValue]==0)
            {
                GYBtnView* view=[selfp viewWithTag:300];
                [view setBtnImage:@"Star"];
                isCollect=YES;
            }else
            {
                GYBtnView* view=[selfp viewWithTag:300];
                [view setBtnImage:@"Star2"];
                isCollect=NO;
            }
        }
    }];
}
@end
